package alexandria.dashboard

import alexandria.dashboard.entity.*

class DataService {
    static transactional = true
	def connectorFactoryService
	def project

    def chartDataSet(c, p) {
		project = p
		println("${c.name}")
		switch (c.name) {
			case "task_vs_status":
				def dataSet = buildDataSet(c)
				dataSet.cols =	[
									[format:"string", name:"Status"],
									[format:"number", name:"Tasks"]
				]
				dataSet.rows =	tasksStatus()

				return dataSet
			break

            case "task_vs_project":
				def dataSet = buildDataSet(c)
				dataSet.cols =	[
									[format:"string", name:"Project"],
									[format:"number", name:"Tasks"]
				]
				dataSet.rows =	[
									[project.name, numTasks()]
				]
                return dataSet
			break

			case "metrics":
				def dataSet = buildDataSet(c)
				dataSet.cols =	[
									[format:"string", name:"Metric"],
									[format:"number", name:"Value"]
				]
				dataSet.rows =	projectMetrics()
                return dataSet
			break

			case "stories_efforts":
				def dataSet = buildDataSet(c)
				dataSet.cols =	[
									[format:"string", name:"Stories"],
									[format:"number", name:"Effort"]
				]
				dataSet.rows =	[
									["${project.name}", storiesEfforts()]
				]
				return dataSet
			break

        }
    }

	def buildDataSet(c){
		def chartDataSet =[
					type:	c.type,
					title:	c.title,
					name:	c.name,
					size:	[width:400, height:300],
					cols:	null,
					rows:	null

				]
		return chartDataSet
	}

	def numTasks(){
		def numTasks = 0
		project.stories.each{
			it.tasks.each{
				numTasks++
			}
		}
		return numTasks
	}

	def tasksStatus(){
		def numComplete=0
		def numIncomplete=0
		project.stories.each{
			it.tasks.each{
				if (it.status=="incomplete")
					numIncomplete++
				else if (it.status=="complete")
					numComplete++
			}
		}
		return [["Complete", numComplete], ["Incomplete", numIncomplete]]
	}

	def projectMetrics(){
		def m = []
		Map metrics = project.metrics
		metrics.each{ key, value ->
			m.add([key.toString(), value])
		}
		return m
	}

	def storiesEfforts(){
		def effort=0
		project.stories.each{
			if (it.size!="")
				effort+=it.size.toInteger()
		}
		return effort
	}


}
